<template>
    <input type="button" value="" :id="this.dateInfo.id" class="form-control">
</template>
<script>
export default {
      name:'dateComponent',
      props:{
            dateInfo:{
                  //{“dateType”:M/m/w/d,'dateValue':20190501~20190502,'id':'id'}id非必传
                  type:Object,
                  required:true,
                  //验证函数
                  validator:function(value){
                        if(!value.dateType){
                              value.dateType='M';
                        }
                        if(!value.dateValue){
                              var today = new Date().valueOf();
                              today = today + -1 * 24 * 60 * 60 * 1000;
                              var endtime = new Date(today).getFullYear()+''+(Array(2).join(0) + (new Date(today).getMonth()+1)).slice(-2)+''+(Array(2).join(0) + (new Date(today).getDate()));
                              today = new Date().valueOf();
                              today = today  - 24 * 60 * 60 * 1000*365*3;
                              var startTime = new Date(today).getFullYear()+''+(Array(2).join(0) + (new Date(today).getMonth()+1)).slice(-2)+''+(Array(2).join(0) + (new Date(today).getDate()));
                              value.dateValue = startTime + '~' + endtime;
                        }
                        if(!value.id){
                              value.id = 'dateButton';
                        }
                        return value;
                  }
            },
            isfav:{
                  type:Object
            },
            company:{
                  type:String
            }
      },
      data:function(){
            return {
                  //日期插件显示范围
                  maxDate:'',
                  minDate:'',
                  //周数据
                  weekData:'',
                  //用于向后端传值，记录时间类型
                  timeType:'',
                  initStartTime:'',
                  initEndTime:'',
                  //请求地址
                  urlList:{
                        getWeekInfo:'/suntorylink/module/CGI/COMMON.WEEK_ALL.CGI',
                        getDateLimit:'/suntorylink/module/CGI/DATE_START_END.CGI',
                        checkDateLimit:'/suntorylink/module/CGI/DATE_CHECK.CGI'
                  },
                  //用于date日期的处理
                  dateType:'',
                  dateValue:'',
                  defaultDay:new Date(),
                  defaultMinDay:new Date(),
                  defaultMaxDay:new Date()
            };
      },
      watch:{
            dateInfo:{
                  handler(){
                        this.dateType = this.dateInfo.dateType;
                        this.dateValue = this.dateInfo.dateValue;
                  },
                  deep:true
            },
            listenChange(newValue, oldValue){
                  if(this.isfav.flag || newValue.company !== oldValue.company 
                  || newValue.dateType !== oldValue.dateType){
                        this.initDateComponents();
                  }
            }

      },
      computed:{
            listenChange () {
                  const {company,dateType,dateValue} = this;
                  return {company,dateType,dateValue};
            }
      },
      methods:{
            dealTimeToString(){
                  return this.$utils.getYestDayDateString();
            },
            //初始化时间插件
            initDateComponents(){
                  this.dealCompanyDataDate();
                  var startDate = '';
                  var endDate = '';
                  if(this.isfav.flag){
                        startDate = this.dateInfo.dateValue.toString().split('~')[0];
                        endDate = this.dateInfo.dateValue.toString().split('~')[1];
                        this.isfav.flag = false;
                  }else{
                        startDate = this.$utils.string2Date(this.initStartTime);
                        endDate = this.$utils.string2Date(this.initEndTime);
                  }
                  var param = {
                        maxDate:this.maxDate === '0'?this.defaultMaxDay:this.$utils.string2Date(this.maxDate),//最大日付
                        minDate:this.minDate === '0'?this.defaultMinDay:this.$utils.string2Date(this.minDate),//最小日付
                        startDate:this.maxDate === '0'?this.defaultDay:startDate,//黙認開始日付
                        endDate:this.maxDate === '0'?this.defaultDay:endDate,//黙認終了日付
                        autoApply:true,//[確定]、[キャンセル]が表示しない設定
                        locale : {
                              separator : '~',
                              applyLabel : '確定',
                              cancelLabel : 'キャンセル',
                              monthNames : [ '1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月' ]
                        }
                  };
                  if(this.dateInfo.dateType === 'm' || this.dateInfo.dateType === 'M'){
                        param.locale.format = 'YYYYMM';
                        param.yearMonthShow=true;
                        param.endYear=true;
                        param.showYearDropdowns=true;
                        param.isinitYM=true;
                        param.maxMoneyYearMonth=this.maxDate === '0'?this.$utils.date2String(this.defaultMaxDay).substr(0,6):this.maxDate.substr(0,6);
                        param.minMoneyYearMonth=this.minDate === '0'?this.$utils.date2String(this.defaultMinDay).substr(0,6):this.minDate.substr(0,6);
                        this.timeType = 'month';
                  }else if(this.dateInfo.dateType === 'w'){
                        param.showDropdowns=true;
                        param.showWeekNumbers = true;
                        param.weekSelect=true;
                        param.yearWeekStr=this.weekData;
                        param.locale.format = 'YYYYWW';
                        param.locale.firstDay = 1;
                        param.locale.daysOfWeek =['日', '月', '火', '水', '木', '金', '土'];
                        param.locale.weekLabel='週';
                        this.timeType = 'week';
                  }else{
                        param.autoclose=true;
                        param.showDropdowns=true;
                        param.locale.format = 'YYYYMMDD';
                        param.locale.firstDay = 1;
                        param.locale.daysOfWeek =['日', '月', '火', '水', '木', '金', '土'];
                        param.locale.weekLabel='週';
                        this.timeType = 'day';
                  }
                  window.$('#'+this.dateInfo.id).daterangepicker(param);
            },
            //获取周信息
            getWeekDateInfo(){
                  return this.$http.post(this.urlList.getWeekInfo);
            },
            //获取时间范围
            getDateLimit(){
                  return this.$http.post(this.urlList.getDateLimit);
            },
            //判断选择时间是否超过一年
            checkDateLimit(){
                  var vm = this;
                  var localDateValue = window.$('#'+vm.dateInfo.id).val();
                  this.$http.post(vm.urlList.checkDateLimit,{'date':localDateValue,'type':vm.timeType})
                        .then(function(response){
                              if(response.data.trim() !== localDateValue){
                                    // vm.initDateComponents();
                                    global.msg_instance.showMsgNew({
                                          title : 'COMMON_022'
                                    });
                                    vm.dateInfo.dateValue = response.data.trim();
                                    // vm.dateInfo.dateValue = response.data.trim();
                              }else{
                                    vm.isfav.flag = false;
                                    vm.dateInfo.dateValue = response.data.trim();
                              }
                        });
            },
            dealCompanyDataDate(){
                  var dateValue = '0';
                  for (var index = 0; index < global.companyOptions.length; index++) {
                        if(this.company === global.companyOptions[index].id){
                              if(this.dateInfo.dateType === 'm'){
                                    dateValue = global.companyOptions[index].periodFiscal;
                              }else{
                                    dateValue = global.companyOptions[index].period;
                              }
                              break;
                        }
                  }
                  if(dateValue === '0' ){
                        this.maxDate = '0';
                        this.minDate = '0';
                  }else{
                        this.minDate = dateValue.toString().split('~')[0];
                        this.maxDate = dateValue.toString().split('~')[1];
                        this.initStartTime = dateValue.toString().split('~')[1];
                        this.initEndTime = dateValue.toString().split('~')[1];
                  }
            }

      },
      mounted(){
            //查询必要数据
            var that = this;
            this.$http.all([that.getWeekDateInfo()])
                  .then(that.$http.spread(function(localWeekInfo){
                        that.weekData = localWeekInfo.data;
                        that.defaultDay = new Date(that.weekData.split('@')[0].split('<->')[3]);
                        that.defaultMinDay = new Date(that.defaultDay.getTime() - 24 - 60 - 60 * 1000);
                        that.defaultMaxDay = new Date(that.defaultMinDay.getTime() - 24 - 60 - 60 * 1000);
                  }));
            window.$('#'+this.dateInfo.id).change(function(){
                  if(window.$('#'+that.dateInfo.id).val() !== that.dateInfo.dateValue.toString()){
                        that.isfav.flag = true;
                        that.checkDateLimit();
                  }
            });
      }
};
</script>
<style scoped>

</style>